package com.example.music_mediaplayer.tool;

import android.os.Handler;
import android.util.Log;

import com.example.music_mediaplayer.ApiInfo;
import com.example.music_mediaplayer.api.BaseApi;
import com.example.music_mediaplayer.gson.GsonUtil;
import com.example.music_mediaplayer.model.JBase;
import com.example.music_mediaplayer.model.JsonBase;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import static com.zhy.http.okhttp.OkHttpUtils.get;
import static com.zhy.http.okhttp.OkHttpUtils.post;

/**
 * Created by quan on 2018/1/5.
 */

public class OkHttpTools {
    private BaseApi baseApi;
    private Handler handler;
    /**
     * 网易搜索音乐
     * @param baseApi
     * @param type
     * @param search_type
     * @param s
     */
    public void wangYiSearchSong(final BaseApi baseApi , String type , String search_type , String s, final int flg){
        this.baseApi = baseApi;
        get()
                .url(ApiInfo.wangYiSearch)
                .addParams("type",type)
                .addParams("search_type",search_type)
                .addParams("s" ,s)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("wangyi",e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
//                        LogoutT.logout(response);
                        Log.i("wangyi",response);
                        JsonBase jsonBase = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(jsonBase.getCode(),jsonBase,flg);
                    }
                });
    }

    /**
     * 网易通过id获取歌曲的url
     * @param baseApi
     * @param type
     * @param id
     */
//    public void getSongUrlById(final BaseApi baseApi , String type , String id){
//        this.baseApi = baseApi;
//        get()
//                .url(ApiInfo.wangYiSongUrl)
//                .addParams("type",type)
//                .addParams("id",id)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        LogoutT.logout(e+"");
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        LogoutT.logout(response);
////                        JBase jBase = GsonUtil.getIns().parseJsonWithGson(response,JBase.class);
//                        JsonBase jsonBase = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
//                        LogoutT.logout(jsonBase.getCode()+"6666666666");
//                        LogoutT.logout(jsonBase.getData().get(0).getUrl().toString()+"747474");
////                        baseApi.backMsg1(jBase.getCode(),jBase);
//                        baseApi.backMsg(jsonBase.getCode(),jsonBase);
//                        LogoutT.logout("0000000");
//
////                        Message msg = new Message();
////                        msg.obj = response;
////                        msg.what = 1;
////                        handler.sendMessage(msg);
//                    }
//                });
//    }

    /**
     * 百度搜索音乐
     * @param baseApi
     * @param query
     * @param flg
     */
    public void baiDuSearchSong(final BaseApi baseApi , String query, final int flg){
        this.baseApi = baseApi;
        post()
                .url("http://www.xiami.com/song/playlist-default/cat/json?_ksTS=1511418676536_377&callback=jsonp378")
                .addParams("query",query)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout(e+"321321");
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout(response+"333333");

                    }
                });
    }


    /**
     * 酷我搜索音乐
     * @param baseApi
     * @param all
     * @param pn
     * @param rn
     * @param flg
     */
    public void kuWoSearchSong(final BaseApi baseApi , String all , String pn , String rn , final int flg){
        this.baseApi = baseApi;
        get()
                .url(ApiInfo.kuWoSearch)
                .addParams("all",all)
                .addParams("pn",pn)
                .addParams("rn",rn)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
//                        LogoutT.logout(response+"321321");
                        JsonBase jsonBase = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                        baseApi.backMsg(jsonBase.getPN(),jsonBase,flg);
                    }
                });
    }

    /**
     * 酷狗音乐搜索接口
     * @param baseApi
     * @param keyword
     * @param page
     * @param pagesize
     */
    public void kuGouSearchSong(final BaseApi baseApi , String keyword , String page , String pagesize , final int flg){
        this.baseApi = baseApi;
        get()
                .url(ApiInfo.kuGouSearch)
                .addParams("keyword",keyword)
                .addParams("page",page)
                .addParams("pagesize",pagesize)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout(e+"321321");
                    }

                    @Override
                    public void onResponse(String response, int id) {
//                        LogoutT.logout(response+"321321");
                        String str = response.substring(0,response.length()-1);
                        String result = str.substring(1,str.length());
                        JBase jBase = GsonUtil.getIns().parseJsonWithGson(result,JBase.class);
                        baseApi.backMsg(jBase.getStatus(),jBase,flg);
                    }
                });
    }

    public void QQSearchSong(final BaseApi baseApi , String w , String p , String n , final int flg){
        this.baseApi = baseApi;
        get()
                .url(ApiInfo.QQSearch)
                .addParams("w",w)
                .addParams("p",p)
                .addParams("n",n)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogoutT.logout(e+"mmmmm");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogoutT.logout(" 1212 1212"+response);
                        String str = response.substring(response.indexOf("{"),response.length()-1);
                        LogoutT.logout("   str  str "+str);
                    }
                });
    }

}
